/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-02 17:24:36
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-27 09:08:45
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { finalize } from 'rxjs/operators';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/api';
import { PartsDeliveryServiceProxy, WorkMatching } from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'app-back-warehouse',
    templateUrl: './back-warehouse.component.html',
    styleUrls: ['./back-warehouse.component.less']
})
export class BackWarehouseComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    @Input() table: [];  //表格数据
    @Input() choose: [];  //如果选择零件了带出数据
    @Input() flag: any;  //操作0 新增查看 1 新增选择 2修改查看 3 修改选择  4退库查看  5 退库选择 6退库记录查看
    @Input() checktype: any
    @Input() parentData: any //修改父级信息

    saving = false;
    active = false;
    addFlag = false;
    selectRow: any = [];
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _partsDeliveryServiceProxy: PartsDeliveryServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    save() {
        let data = { parent: {}, choose: new WorkMatching(), table: [] };
        let son = new WorkMatching();
        son.loadDetailsId = this.editData.node.data.loadDetailsId;
        son.storeIdList = [];
        for (const item of this.selectRow) {
            son.storeIdList.push(item.id);
        }
        data.choose = son;
        data.parent = this.editData;
        data.table = this.table;
        this.modalSave.emit(data);
        this.close();
    }
    getTableList(event?: LazyLoadEvent) {
        this._partsDeliveryServiceProxy.getMaterialDetails(
            this.parentData,//修改时候传表格数据ID
            this.editData.node.data.id,
            this.editData.node.data.loadDetailsId,
            this.flag
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.records = result.materialDetails;
            this.selectRow = [];
            for (const parent of this.choose) {
                let same = this.primengTableHelper.records.filter(item => item.id == parent);
                if (same.length > 0) {
                    this.selectRow.push(same[0]);
                }
            }
            if (this.choose.length == 0) {
                for (const item of result.materialDetails) {
                    if (item.canBeChoose) {
                        this.selectRow.push(item);
                    }
                }
            }
        });
    }
    //打开弹窗
    onShown(): void {
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.active = false;
        this.primengTableHelper.records = [];
        this.selectRow = [];
        let data = { parent: {}, table: [] };
        data.parent = null;
        data.table = this.table;
        this.modalSave.emit(data);
        this.modal.hide();
    }
}
